package com.siblings.loan.ui

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.lib.base.base.BaseActivity
import com.siblings.loan.R
import com.siblings.loan.databinding.ActPaPoyBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.utils.dialog.KefuDialog
import com.siblings.loan.utils.event.SibEventType
import com.siblings.loan.utils.helper.SharedHelper
import org.json.JSONObject

class PaPoyAct : BaseActivity() {

    private lateinit var binding: ActPaPoyBinding

    override fun getSibRootView(): View {
        binding = ActPaPoyBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initSibView() {
        setSibTitleBarVisibility(View.VISIBLE)
        setSibTextTitle(getString(R.string.privacy_policy_title))
        setSibTextTitleGravity(Gravity.CENTER)
        setSibRightBtnRes(com.lib.base.R.mipmap.icon_kefu_black)

        initWebViewSettings()
    }

    override fun initSibListener() {
        //联系客服
        rootViewBinding.ivFun.setOnClickListener {
            showSibKefuDialog("Privacy Policy Page")
            HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_CUSTOMER,"Privacy Policy Page")
        }
    }

    override fun initSibData() {
        val commonData = JSONObject(SharedHelper.getSibSPString(SharedHelper.SIB_SPKEY.SIBLINGS_COMMON))
        val customize = commonData.optJSONObject("customize")
        if (customize != null) {
            val privacyPolicyLink = customize.optString("privacyPolicyLink")
            binding.ppWebView.loadUrl(privacyPolicyLink)
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        binding.ppWebView.setInitialScale(100)
        binding.ppWebView.webChromeClient = mWebChromeClient
        binding.ppWebView.webViewClient = mWebClient
        val webSettings = binding.ppWebView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            binding.progressBar.progress = newProgress
            if (newProgress == 100) {
                binding.progressBar.visibility = View.GONE
            } else {
                binding.progressBar.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?,
        ): Boolean {
            return shouldOverrideUrlLoading(view, request!!.url.toString())
        }

        @Deprecated("Deprecated in Java", ReplaceWith("false"))
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }

    /**
     * 显示客服dialog
     */
    private fun showSibKefuDialog(eventRemark: String){
        val kefuDialog = KefuDialog(this,eventRemark,object : KefuDialog.IDialogCallback{
            override fun onClick(view: View?) {
            }
        })
        kefuDialog.setCancelable(true)
        kefuDialog.setCanceledOnTouchOutside(true)
        kefuDialog.show()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_PRIVACY_POLICY_START,"")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_PRIVACY_POLICY_END,"")
    }
}